<?php

namespace app\api\controller\v1;

use app\common\controller\BaseController;
use think\Controller;
use think\Request;
use app\api\model\RemindCourse as RemindCourseModel;
use app\api\model\RemindOrder as RemindOrderModel;

class Remind extends BaseController
{
    protected $noNeedLogin = [''];

    /**
     * 提醒列表
     */
    public function index()
    {
        $userId = $this->userId;
        //课程提醒
        $course = RemindCourseModel::getByUserId($userId);
        $courseReading = RemindCourseModel::getReadingByUserId($userId);
        //订单提醒
        $order = RemindOrderModel::getByUserId($userId);
        $orderReading = RemindOrderModel::getReadingByUserId($userId);
        $this->success('', [
            'course' => $course,
            'course_reading' => $courseReading ?: 0,
            'order' => $order,
            'order_reading' => $orderReading ?: 0
        ]);
    }

    /**
     * 课程提醒列表
     */
    public function course()
    {
        $userId = $this->userId;
        $size = $this->request->get('size');
        $courses = RemindCourseModel::getAllByUserId($userId, $size);
        $courses->total() ? $this->success('', [
            'courses' => $courses->items(),
            'total' => $courses->total()
        ]) : $this->notFoundData();
    }

    /**
     * 订单提醒列表
     */
    public function order()
    {
        $userId = $this->userId;
        $size = $this->request->get('size');
        $courseOrder = RemindOrderModel::getAllByUserId($userId, $size);
        $courseOrder->total() ? $this->success('', [
            'course_order' => $courseOrder->items(),
            'total' => $courseOrder->total()
        ]) : $this->notFoundData();
    }

    /**
     * 课程已读
     */
    public function courseReading()
    {
        $remind_course_id = $this->notEmpty('remind_course_id');
        $remindCourse = RemindCourseModel::find($remind_course_id);
        if (!$remindCourse) {
            $this->errorMsg(17000);
        }
        if ($remindCourse->reading) {
            $this->success();
        }
        $remindCourse->reading = 1;
        $res = $remindCourse->save();
        $res ? $this->success() : $this->forbidden();
    }

    /**
     * 课程已读
     */
    public function orderReading()
    {
        $remind_order_id = $this->notEmpty('remind_order_id');
        $remindOrder = RemindOrderModel::find($remind_order_id);
        if (!$remindOrder) {
            $this->errorMsg(17001);
        }
        if ($remindOrder->reading) {
            $this->success();
        }
        $remindOrder->reading = 1;
        $res = $remindOrder->save();
        $res ? $this->success() : $this->forbidden();
    }
}
